#' @author edvard
#' @time 2017.03.28

final.data <- read.csv(paste(user_data_path, "context_log.csv", sep = ""), sep = ",", header = T, stringsAsFactors = F)
app.info <- read.csv("cache/app_info.csv", sep = ",", header = T, stringsAsFactors = F)

# Transfer dataframe into list
list.final.data <- list()

# Get time_segment_no factor
time.no <- c()
time.no <- c("Weekend", "Workday", "Night","Morning","Noon", "Afternoon", "Evening", time.no)
time.no <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", time.no)

final.data$date <- as.character(final.data$date)
final.data$time_segment_no <- as.character(final.data$time_segment_no)
final.data$location <- as.character(final.data$location)
final.data$application <- as.character(final.data$application)
final.data$weekday <- as.character(final.data$weekday)
final.data$user_status <- as.character(final.data$user_status)
final.data$holiday <- as.character(final.data$holiday)
final.data$period <- as.character(final.data$period)

final.data$location <- ifelse(final.data$location == "loc_", "", final.data$location)

for(i in 1:dim(final.data)[1]){
  transaction <- c()
  if(final.data[i, 3] != "")
    transaction <- c(transaction, final.data[i, 3])
  
  if(final.data[i, 4] != "")
    transaction <- c(transaction, strsplit(final.data[i, 4], "=>") %>% unlist())
  
  if(final.data[i, 6] != "")
    transaction <- c(transaction, final.data[i, 6])
  
  if(final.data[i, 7] != "")
    transaction <- c(transaction, final.data[i, 7])
  
  if(final.data[i, 8] != "")
    transaction <- c(transaction, final.data[i, 8])
  
  if(final.data[i, 9] != "")
    transaction <- c(transaction, final.data[i, 9])
  
  list.final.data[[i]] = transaction
}

# Transfer list into transaction
transaction.final.data <- as(list.final.data, 'transactions')
dim(transaction.final.data)
colnames(transaction.final.data)
itemFrequencyPlot(transaction.final.data, support = 0.0001, topN = 50, horiz = T)

# a.one <- apriori(transaction.final.data, parameter = list(support = 0.002, minlen = 2, maxlen = 10, target = "frequent"))
# a.two <- apriori(transaction.final.data, parameter = list(support = 0.004, minlen = 2, maxlen = 10))

# Get rule according to day
res <- apriori(transaction.final.data, parameter = list(supp = 0.003, conf = 0.005, minlen = 2, maxlen = 10), appearance = list(lhs = time.no, default = "rhs"))
# oder by lift
res <- sort(res, by = "confidence")
periodic.patterns <- as(res, "data.frame")
rownames(periodic.patterns) <- NULL 
# MPA
# Store the periodic pattern
write.csv(periodic.patterns[, 3], "res/mpa_edvard.csv", row.names = F)
dw.periodic.patterns <- read.csv("res/mpa_edvard.csv", header = T, stringsAsFactors = F)

# DW-PPM
# periodic.patterns <- periodic.patterns %>% filter(lift >= 3)
# Get rhs
dw.periodic.patterns$rules <- dw.periodic.patterns$rules %>% as.character()
rhs <- c()
for(i in 1:dim(dw.periodic.patterns)[1]){
  tmp <- strsplit(dw.periodic.patterns[i,1], "=>")[[1]][2]
  rhs[i] <- substr(tmp, 3, nchar(tmp) - 1)
}
dw.periodic.patterns <- data.frame(dw.periodic.patterns, app_name = rhs, stringsAsFactors = F)
dw.periodic.patterns <- data.frame(dw.periodic.patterns, inverse_weight = 0, stringsAsFactors = F)
for(i in 1:dim(dw.periodic.patterns)[1]){
  tmp = app.info %>% filter(app_name == dw.periodic.patterns[i, 5]) %>% select(inverse_weight) %>% as.numeric()
  dw.periodic.patterns[i, dim(dw.periodic.patterns)[2]] <- ifelse(is.na(tmp), 0, tmp * dw.periodic.patterns[i, 3] %>% as.numeric())
}
dw.periodic.patterns <- arrange(dw.periodic.patterns, desc(inverse_weight))
# dw.periodic.patterns <- dw.periodic.patterns %>% filter(lift >= 1.9)
write.csv(dw.periodic.patterns, "res/dw_ppm_edvard.csv", row.names = F)






